package com.ruoyi.web.controller.kyhs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.kysh.entity.DrillInf;
import com.ruoyi.system.kysh.service.DrillInfService;
import com.ruoyi.system.kysh.utils.MyTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/kyhs/drill")
public class DrillInfController {

    @Autowired
    private DrillInfService drillInfService;

    /**
     *
     * @param page 当前页
     * @param pageSize 每页数量
     * @return 结果
     */
    @GetMapping("/page")
    public AjaxResult taskPage(Long id,int page,int pageSize){

        //分页构造器
        Page<DrillInf> pageInfo=new Page<>(page,pageSize);
        //进行分页查询
        LambdaQueryWrapper<DrillInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //只查询未删除信息
        lambdaQueryWrapper.eq(DrillInf::getDeleteLogic,"0");

        lambdaQueryWrapper.orderByDesc(DrillInf::getUpdateTime);
        lambdaQueryWrapper.orderByDesc(DrillInf::getCreateTime);

        if(id!=null && !"".equals(id.toString())){
            lambdaQueryWrapper.eq(DrillInf::getBorerId,id);
        }
        drillInfService.page(pageInfo,lambdaQueryWrapper);

        return AjaxResult.success("操作成功",pageInfo);
    }

    /**
     *
     * @param id 钻孔编号
     * @return 结果
     */
    @GetMapping("/select-by-id")
    public AjaxResult getTaskById(Long id){
        if(id==null || "".equals(id.toString())){
            return AjaxResult.warn("请输入相应信息");
        }

        LambdaQueryWrapper<DrillInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //查询对应id且未删除信息
        lambdaQueryWrapper
                .eq(DrillInf::getBorerId,id)
                .eq(DrillInf::getDeleteLogic,"0");

        DrillInf oneTask = drillInfService.getOne(lambdaQueryWrapper);
        return AjaxResult.success("操作成功",oneTask);
    }

    /**
     *
     * @param drillinf 所删除对象
     * @return 删除是都成功
     */
    @DeleteMapping("/delete-one")
    public AjaxResult deleteOne(@RequestBody DrillInf drillinf){
        LambdaQueryWrapper<DrillInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DrillInf::getBorerId, drillinf.getBorerId());
        DrillInf oneTask = drillInfService.getOne(lambdaQueryWrapper);
        if(oneTask.getDeleteLogic().equals("1")){
            return AjaxResult.error("已删除，请勿重复操作");
        }
        oneTask.setDeleteLogic("1");//已删除状态
        boolean update = drillInfService.updateById(oneTask);
        if(update){
            return AjaxResult.success("操作成功");
        }else {
            return AjaxResult.error("未知错误");
        }
    }

    /**
     *
     * @param drillinf 所新建对象
     * @return 是否成功
     */
    @PostMapping("/save-one")
    public AjaxResult saveOne(@RequestBody DrillInf drillinf){
        //是否已经存在
        LambdaQueryWrapper<DrillInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DrillInf::getBorerId, drillinf.getBorerId());
        DrillInf oneTask = drillInfService.getOne(lambdaQueryWrapper);

        drillinf.setCreateTime(MyTimeUtil.nowTime());
        drillinf.setUpdateTime(MyTimeUtil.nowTime());

        //存在则进行更新
        if(oneTask!=null){
            if(oneTask.getDeleteLogic().equals("0")){
                return AjaxResult.warn("当前钻孔编号已存在");
            }
            drillinf.setDeleteLogic("0");//修改逻辑删除状态

            drillinf.setId(oneTask.getId());

            drillInfService.updateById(drillinf);
            return AjaxResult.success("新建成功");
        }

        //不存在则进行存储
        drillInfService.save(drillinf);

        return AjaxResult.success("新建成功");
    }

    /**
     *
     * @param drillinf 所编辑对象
     * @return 编辑结果
     */
    @PutMapping("/update-one")
    public AjaxResult updateOne(@RequestBody DrillInf drillinf){
        //判断钻孔编号不重复
        LambdaQueryWrapper<DrillInf> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DrillInf::getBorerId,drillinf.getBorerId());
        DrillInf drillInf1 = drillInfService.getOne(queryWrapper);
        if(drillInf1!=null&& !drillInf1.getId().equals(drillinf.getId())){
            return AjaxResult.warn("该钻孔编号已存在");
        }

        LambdaQueryWrapper<DrillInf> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DrillInf::getId, drillinf.getId());

        drillinf.setUpdateTime(MyTimeUtil.nowTime());

        boolean update = drillInfService.update(drillinf, lambdaQueryWrapper);
        if(update){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error();
        }
    }

}
